﻿using ScanSnap.Domain.JSON;
using ScanSnap.Util;
using ScanSnap.Wpf.WpfPage;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ScanSnap.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for StartingPage.xaml
    /// </summary>
    public partial class StartingPage : Page
    {
        private static StartingPage instance;
        private bool IsActive = false;

        private StartingPage()
        {
            InitializeComponent();

            CopyrightLabel.Content = "www.axiohelix.com, Copyright ©2013-" + DateTime.Now.Year +" All Rights Reserved.";
            
            IList<string> errorList = ValidateUtility.ValidateSettings(ApplicationSettings.SERVER_URL, ApplicationSettings.SCAN_FOLDER);
            
            if (errorList.Count > 0)
            {
                SettingsPage.Instance.ErrorList = errorList;
                ContentFrame.Content = SettingsPage.Instance;
            }
            else
            {
                ContentFrame.Content = LoginPage.Instance;
            }
            
            instance = this;
        }

        public static StartingPage Instance
        {
            get
            {
                if (instance == null || !instance.IsActive)
                {
                    instance = new StartingPage();
                }

                return instance; 
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
            Environment.Exit(0);
        }

        private void OnDragMoveWindow(object sender, MouseButtonEventArgs e)
        {
            //this.DragMove();
        }

        private void OnMinimizeWindow(object sender, MouseButtonEventArgs e)
        {
           // this.WindowState = WindowState.Minimized;
        }

        private void OnMaximizeWindow(object sender, MouseButtonEventArgs e)
        {
            //if (this.WindowState == WindowState.Maximized)
            //    this.WindowState = WindowState.Normal;
            //else if (this.WindowState == WindowState.Normal)
             //   this.WindowState = WindowState.Maximized;
        }

        private void OnCloseWindow(object sender, MouseButtonEventArgs e)
        {
           // this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
           // this.Close();
        }

        private void SettingsPageButton_Click(object sender, RoutedEventArgs e)
        {
            TransferToSettingsPage();
        }

        private void SignInPageButton_Click(object sender, RoutedEventArgs e)
        {
            TransferToSignInPage();
        }

        public void TransferToSettingsPage()
        {
            //ContentFrame.Content = null;
            ContentFrame.Content = SettingsPage.Instance;
        }

        public void TransferToSignInPage()
        {
            //ContentFrame.Content = null;
            ContentFrame.Content = LoginPage.Instance;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {            
            Application.Current.Shutdown();
            Environment.Exit(0);
        }
    }
}
